/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.dicfile;

import com.cse.p2p.common.DicFile;
import com.cse.p2p.hash.HashImplement;
import com.cse.p2p.localfile.LocalFile;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.logic.ServerLogicImplement;
import server.logic.ServerLogicInterface;

/**
 *
 * @author DUCMINHKHOI
 */
//FILE_ID, FILE_NAME, FILE_SIZE, ClientUpIP, Describe
//TRACKER_URL : IP of Server for communicate
//PIECE_SIZE, NUM_OF_PIECE
//HASH_MAP: type: Map

/**
 *
 * @author DUCMINHKHOI
 *
 */
public class DicFileGenerating {

    /**
     * This method will collect information about the file recently upload the Server and create a DicFile for this
     * @param file
     * @param fileID: file's ID
     * @param fileName: file's name
     * @param describe: file's describe
     * @param clientUpIP: clientUP IP
     * @param tracker_url: Server IP address
     */
    public static DicFile run(File file, int fileID, String fileName,
            String describe, String clientUpIP, String tracker_url) {

        long fileSize = file.length();

        Map hashMap = null;

        int pieceSize = ServerLogicImplement.choosePiecesSize();

        int numberOfPieces = ServerLogicImplement.getNumberOfPiece(fileSize, pieceSize);
        try {
            hashMap = HashImplement.getHashMap(file, pieceSize, numberOfPieces);
        } catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
            Logger.getLogger(DicFileGenerating.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
            Logger.getLogger(DicFileGenerating.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
			ex.printStackTrace();
            Logger.getLogger(DicFileGenerating.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
			ex.printStackTrace();
            Logger.getLogger(DicFileGenerating.class.getName()).log(Level.SEVERE, null, ex);
        }

        DicFile dicFile = new DicFile(tracker_url, fileID, fileName, fileSize, pieceSize, hashMap);
        
        return dicFile;
    }
}
